import {getJson, postJson} from '../utils/request'
import {apiConfig} from '../constants/api'
import Taro from '@tarojs/taro'

export function  accessUserToken(params) {
  return async dispatch => {
    let res = await postJson(apiConfig.getToekn, params)
    if(res && res.data && res.data.success) {
      dispatch({type: 'loginSuccess', accesstoken: params.accesstoken, loginname: res.data.loginname, avatar_url: res.data.avatar_url})
      return true
    }else{
      dispatch({type: 'loginSuccess', accesstoken: null, loginname: null})
    }
    return false
  }
}

export async function getUserInfo(params) {
  let res = await getJson(apiConfig.userInfo + params.loginname)
  if(res && res.data && res.data.success) {
    return res.data
  }else{
    Taro.showToast({title: '获取用户信息失败', icon: 'none'})
  }
  return false
}

export async function validateUser (params) {
  console.log('params = ', params);
  
  if(params && params.accessToken) {
    return true
  }
  return false
}